//一般不改
//  NewNickViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/28.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "NewNickViewController.h"
#import "MyDataViewController.h"
#import "MyCenterViewController.h"

@interface NewNickViewController ()
@property (nonatomic,strong)UITextField *NewNick;
@property (nonatomic,strong)UILabel *label;
@property (nonatomic,strong)UIImageView *backgroundView;
@end

@implementation NewNickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //   背景图片
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.image = [UIImage imageNamed:@"100.jpg"];
    [self.view addSubview:self.backgroundView];
    [self.view sendSubviewToBack:self.backgroundView];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"修改昵称";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(saveAction)];
    
    self.NewNick = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.NewNick.borderStyle = UITextBorderStyleRoundedRect;
    self.NewNick.placeholder = @"请输入新昵称";
    self.NewNick.font = [UIFont systemFontOfSize:20];
    self.NewNick.textColor = [UIColor purpleColor];
    self.NewNick.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.NewNick];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(30, 50, [UIScreen mainScreen].bounds.size.width-60, 40)];
    self.label.text = @"*朋友印象，从昵称开始*";
    self.label.textColor = [UIColor grayColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.label];

}

-(void)backAction{
        [self.navigationController popViewControllerAnimated:YES];
}
//保存修改昵称
-(void)saveAction{
    if (![self.NewNick.text isEqualToString:@""]) {
        
        [[MyDataViewController sharedMyDataViewController].myDataTV reloadData];
        
        //    将新昵称 加入数据库
        AVUser *user = [AVUser currentUser];
            [user setObject:self.NewNick.text forKey:@"nickname"];
            [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"改名成功%@",user[@"nickname"]);
                }
            }];
    }
//    存入本地
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    //    存入数据
    [user1 setObject:self.NewNick.text forKey:@"nickname"];
    //    同步数据
    [user1 synchronize];
    
    [self backAction];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
